package com.hzw.saas.service.group.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.Map;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzw.saas.api.group.IGroupLogService;
import com.hzw.saas.api.group.bo.GroupLogBo;
import com.hzw.saas.api.group.query.GroupLogQuery;
import com.hzw.saas.common.util.IdWorker;
import com.hzw.saas.common.util.Json;
import com.hzw.saas.service.group.mapper.GroupLogMapper;
import com.hzw.saas.service.group.model.GroupLog;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import lombok.AllArgsConstructor;
import ma.glasnost.orika.MapperFacade;

/**
 * <p>
 *  群组动态实现
 * </p>
 *
 * @author sonam
 * @since 2020/12/31 17:26
 */
@AllArgsConstructor
@Service("groupLogServiceImpl")
public class GroupLogServiceImpl extends ServiceImpl<GroupLogMapper, GroupLog> implements IGroupLogService, IService<GroupLog> {

    private final IdWorker idWorker;
    private final MapperFacade mapperFacade;

    @CachePut(cacheNames = {"GroupLog"}, key = "#result.logId")
    @Override
    public GroupLogBo addGroupLog(GroupLogQuery groupLogParam) {

        GroupLog groupLog = mapperFacade.map(groupLogParam, GroupLog.class);
        groupLog.setLogDate(new Date());
        groupLog.setLogId(String.valueOf(idWorker.nextId()));
        this.save(groupLog);

        return mapperFacade.map(groupLog, GroupLogBo.class);
    }

    @CacheEvict(cacheNames = {"GroupLog"}, key = "#logId")
    @Override
    public void deleteGroupLog(String logId) {
        this.removeById(logId);
    }

    @Cacheable(cacheNames = {"GroupLog"}, key = "#logId")
    @Override
    public GroupLogBo getGroupLogById(String logId) {
        GroupLog groupLog = this.getById(logId);
        return mapperFacade.map(groupLog, GroupLogBo.class);
    }

    @Override
    public IPage<GroupLogBo> pageByGroupId(IPage page, String groupId) {
        IPage<GroupLog> groupLogIPage = page(page, Wrappers.<GroupLog>lambdaQuery()
            .eq(GroupLog::getGroupId, groupId).orderByDesc(GroupLog::getLogDate));

        IPage<GroupLogBo> groupLogDtoIPage = new Page<>(groupLogIPage.getCurrent(),groupLogIPage.getSize(),groupLogIPage.getTotal());
        groupLogDtoIPage.setRecords(new ArrayList<>());

        groupLogIPage.getRecords().forEach(groupLog -> {
            GroupLogBo groupLogDto = mapperFacade.map(groupLog, GroupLogBo.class);
            Map map = Json.parseObject(groupLog.getLogMsg(), Map.class);
            groupLogDto.setUsername(String.valueOf(map.get("username")));
            groupLogDto.setMsg(String.valueOf(map.get("msg")));
            groupLogDtoIPage.getRecords().add(groupLogDto);
        });

        return groupLogDtoIPage;
    }
}
